package yanglei.day0110;

import yanglei.day0109.lottery.dao.UserDao;
import yanglei.day0109.lottery.dao.impl.UserDaoImpl;
import yanglei.day0109.lottery.po.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "JstlServlet", value = "/JstlServlet")
public class JstlServlet extends HttpServlet {

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        // 设置中文字符乱乱码，解决request/response响应
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 使用多态体现，实例化 UserDao userDao
        UserDao userDao = new UserDaoImpl();
        // 创建Map集合对象,用来存储查询列表的搜索条件参数
        Map<String, Object> map = new HashMap<>();
        // 获取indexController.jsp中存储在request作用域中的 searchName
        String searchName = request.getAttribute("searchName") == null ? "" :
                String.valueOf(request.getAttribute("searchName"));
        if (!"".equals(searchName)) {
            map.put("markNo", searchName);
        }
        // 调用查询列表方法
        List<User> userList = userDao.queryUserByType(map);
        // 使用jsp提供的EL表达式和JSTL标签库简化Jsp页面中的JavaBean(代码)
        request.setAttribute("userList", userList);
        request.getRequestDispatcher("jsp/yanglei/day0110/jstl.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
